Mihai Ion
  • Home
  • Research
  • Teaching
  • CV
  1. FIN 525
  2. Lectures
  3. L00: Jupyter basics
  • Teaching
  • FIN 525
    • Lectures
      • L00: Jupyter basics
      • L01: Introduction
      • L02: Variables, types, operators
      • L03: Data structures
      • L04: Conditionals, loops
      • L05: Functions, packages
      • L06: Pandas intro
      • L07: Pandas I/O
      • L08: Pandas filtering
      • L09: Pandas data cleaning
      • L10: Merging, reshaping datasets
      • L11: Dates, lags, sorting
      • L12: Descriptive stats
      • L13: Conditional stats, outliers
      • L14: Conditional stats applied
      • L15: Linear regression intro
      • L16: Linear regression applications
      • L17: Panel regression intro
      • L18: Robust panel regression
      • L19: Robust timeseries regression
      • L20: Backtesting - data prep
      • L21: Backtesting - sumstats
      • L22: Backtesting -returns
      • L23: Backtesting - risk adjustment
  • FIN 421
    • Lectures
      • L01: Introduction
      • L02: Analyzing past returns
      • L03: Modeling future returns
      • L04: Portfolio theory intro
      • L05: Optimal capital allocation
      • L06: Tangency portfolios
      • L07_08: Optimal asset allocation
      • L09: Review
      • L10_11: Statistical models of returns
      • L12: CAPM
      • L13: Cost of equity
      • L14: Bond pricing
      • L15: Bond yields
      • L16: Bond risk
      • L17: Valuation data processing
      • L18_19: Multiples valuation
      • L20_21: Dividend discount models
      • L22_23: Discounted cash flow analysis
      • L24: Valuation sensitivity analysis
      • L25: Options intro
      • L26: Risk management with options

On this page

  • CELLS
    • Markdown cells
    • Markdown formatting
  • Largest header
    • Second largest header
      • Third largers header
      • LaTex
    • Code cells
  • Important notebook commands

L00: Jupyter basics

CELLS

Jupyter notebooks are split into “cells”.

If you click anywhere inside a notebook, it will enclose the cell that you clicked on in a rectangle, to show you the exact contents of that cell (the cell may be empty). - You can then press “Enter” to edit the contents of that cell (the enclosing rectangle will change colors - from blue to green). - When you are done editing a cell, you can press “Shift+Enter” to run it.

The two types of cells that you will be using most often are: - Markdown cells - Contain text in “Markdown” language (more on this below) - Convert a cell to Markdown by clicking on it, and the pressing “Esc” and “M” - Code cells - Contain code in language you specified by the notebook’s “kernel” (more on this below) - Convert a cell to Markdown by clicking on it, and the pressing “Esc” and “Y”

Markdown cells

These are cells where you write text. This cell is a Markdown cell. Think of Markdown as a lightweight version of Microsoft Word. It allows you to format your text (indentation, bullet points, italics, etc) using pre-specified syntax (code) rather than clicking through menu items like in Work.

Markdown formatting

The following markdown cell shows you how to do basic formatting in Markdown

Headers:

Largest header

Second largest header

Third largers header

Lists (bullet points):

  • item
    • subitem
      • subsubitem
  • item
    • subitem
      • subsubitem

Lists (numbered):

  1. item
  2. item

Lists (mixed):

  1. item
    • subitem
      • subsubitem
        • subsubsubitem
  2. item
    • subitem
      • subsubitem
        • subsubsubitem

This is how you do italics and this is how you do bold.

LaTex

Markdown also allows you to write mathematical formulas using LaTex syntax.

For example:

\(Y_{i,t} = \alpha + \beta X_{i,t} + \epsilon_{i,t}\)

Which was written as

     $Y_{i,t} = \alpha + \beta X_{i,t} + \epsilon_{i,t}$

Note that the LaTex syntax for the symbols in your equation are different from Markdown syntax.

Code cells

By default, any new cell you create in a notebook will be a code cell.

In a code cell, you write code written in the language of the kernel you chose for your notebook. This is the language you selected when you created your notebook. It is usually displayed in the top right corner of the screen (under the “Logout” icon. If you write code written in any other language in a code cell, you will get errors when you try to run that cell.

For example, this is a Python 3 notebook, so the following code will work because it is written in the Python 3 language:

print("This will work")

But the following code will not work, because it is written in the Stata language:

display "This will not work"

There are some kernels that will allow you to create notebooks that support multiple programming languages, but that is a more advanced topic and we will not cover it here.

Each code cell can contain multiple lines of code.

print("Line 1")
print("Line 2")
a = 2

And the code in a particular code cell will recognize variables that were created in a different code cell (from above or below), as long as that cell has already been run. For example the following line of code knows what the variable “a” is (from the previous cell)

print(a)

To the left of every code cell, you will see the order in which you ran them in the current session. For example, “In [4]” means this is the fourth code cell you ran in this session. Because at any time, you can run any code cell you want, the order in which you run them may not match the order in which they appear in the notebook. I highly recommend you NEVER use in a code cell variables that you define in a cell that appears lower in the notebook. Another way of saying that is to always assume that the code cells will be run in the order in which they appear in the notebook (even though that need not be the case).

For example, don’t run the following cell AFTER you run the one after it:

print(b)
3

Note that code cells don’t always have an output. For example the line below does not seem to do anything when you run it, but it does assign the value 3 to variable b (we’ll talk more about variables next class).

b = 3

Important notebook commands

As mentioned above, after clicking on any command, you can press “Enter” to enter “Edit mode” (to be able to edit the contents of that cell) or “Esc” to enter “Command mode” which allows you to use keyboard shortcuts to ask Jupyter to do specific things.

Here are some of the most common such commands (remember, these will not work unless you pressed “Esc” right before, to enter command mode):

  • press “M” to convert the current cell to a Markdown cell
  • press “Y” to convert the current cell to a Code cell
  • press “A” to insert a new cells right above the current cell
  • press “B” to insert a new cells below above the current cell
  • press “C” to copy the selected cells
  • press “X” to cut the selected cells
  • press “V” to past the selected cells
  • press “D” + “D” to delete selected cells
  • press “Z” to undo cell deletion
  • press “Ctrl” + “Enter” to run the selected cells
  • press “0” (zero, not the letter O) to restart your kernel (this “forgets” all the code you ran in the current session)

To see all the available commands, press “Ctrl” + “Shift” + “P”